package com.xci.platform.auth.controller;

import com.xci.platform.annotation.Authorize;
import com.xci.platform.auth.entity.RoleEntity;
import com.xci.platform.auth.service.RoleService;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.ExcelHelper;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 系统角色控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/role")
public class RoleController extends AuthBaseController {
    /**
     * 角色服务
     */
    @Resource
    private RoleService roleService;

    //region 页面视图

    /**
     * 角色首页
     */
    @GetMapping
    @Authorize(code = "SystemRole")
    public String index() {
        return "auth/role/index";
    }

    /**
     * 角色新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemRole.Create")
    public String create(ModelMap map) {
        createMark(map);
        RoleEntity entity = new RoleEntity();
        entity.setId(StringHelper.guid());
        entity.setStatus(1);
        map.put("entity", entity);
        return "auth/role/edit";
    }

    /**
     * 角色编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemRole.Edit")
    public String edit(String id, ModelMap map) {
        RoleEntity entity = roleService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/role/edit";
    }

    /**
     * 角色详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        RoleEntity entity = roleService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        map.put("dataMessage", roleService.queryOwnDataSetting(entity.getId()));
        return "auth/role/details";
    }

    //endregion

    //region 数据处理

    /**
     * 角色表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemRole.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(roleService.queryPageList(params));
    }

    /**
     * 保存新增角色
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemRole.Create")
    public BoolMessage createSave(@ModelAttribute RoleEntity entity) {
        return roleService.save(entity, true);
    }

    /**
     * 保存修改角色
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemRole.Edit")
    public BoolMessage editSave(@ModelAttribute RoleEntity entity) {
        return roleService.save(entity, false);
    }

    /**
     * 修改角色启用状态
     */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "SystemRole.Status")
    public BoolMessage status(String id, Integer status) {
        return roleService.updateStatus(id, status);
    }

    /**
     * 删除角色
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemRole.Delete")
    public BoolMessage delete(String ids) {
        return roleService.delete(StringHelper.splitToArray(ids));
    }

    /**
     * 导出角色
     */
    @GetMapping("/export")
    @Authorize(code = "SystemRole.Export")
    public void export(@RequestParam Map params, HttpServletResponse response) throws IOException {
        ExcelHelper.exportWeb(response, roleService.queryList(params), RoleEntity.class, "系统角色");
    }
    //endregion
}